﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Converter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        CustomDecimal dec = new CustomDecimal();
        Binary binary = new Binary();
        HexaDecimal hex = new HexaDecimal();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void TextBox_SelectAll(object sender, RoutedEventArgs e)
        {
            TextBox tb = (sender as TextBox);
            if (tb != null)
            {
                tb.SelectAll();
            }
        }

        private void ProcessInput(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                TextBox tb = (sender as TextBox);
                String sInput = tb.Text;

                if (tb.Text != null)
                {
                    Regex regex = new Regex(@"\s");
                    string[] sArrInput = regex.Split(sInput);

                    try
                    {
                        if (tb.Name == "TxtDecimal")
                        {
                            TxtBinary.Text = dec.DecimalToBinary(sArrInput);
                            TxtHex.Text = dec.DecimalToHex(sArrInput);
                        }
                        else if (tb.Name == "TxtHex")
                        {
                            TxtBinary.Text = hex.HexToBinary(sArrInput);
                            TxtDecimal.Text = hex.HexToDecimal(sArrInput);
                        }
                        else if (tb.Name == "TxtBinary")
                        {
                            TxtDecimal.Text = binary.BinaryToDecimal(sArrInput);
                            TxtHex.Text = binary.BinaryToHex(sArrInput);
                        }
                        else if (tb.Name == "TxtExtAscii")
                        {
                            
                        }
                    }
                    catch
                    {
                        UpdateErrorData(tb.Name);
                    }
                }
            }
        }

        private void UpdateErrorData(string sName)
        {
            foreach (Control c in LayoutRoot.Children)
            {
	            if(c.GetType() == typeof(TextBox))
	            {
		            TextBox tb = (TextBox)c;
                    if (!(tb.Name == sName))
                        tb.Text = "ERR";
	            }
            }
            LblMessage.Content = "Provide a valid input";
        }

        /*private void TxtHex_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                TextBox tb = (sender as TextBox);
                if (tb.Text != null)
                {
                    try
                    {
                        TxtDecimal.Text = Convert.ToString(Convert.ToInt32(tb.Text, 16));
                        TxtBinary.Text = Convert.ToString(Convert.ToInt32(tb.Text, 16), 2);
                    }
                    catch (Exception)
                    {
                        LblMessage.Content = "Provide a valid hex input.";
                        TxtDecimal.Text = "ERR";
                        TxtBinary.Text = "ERR";
                    }
                }
            }
        }

        private void TxtBinary_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                TextBox tb = (sender as TextBox);
                if (tb.Text != null)
                {
                    try
                    {
                        TxtDecimal.Text = Convert.ToString(Convert.ToInt32(tb.Text, 2));
                        TxtHex.Text = Convert.ToString(Convert.ToInt32(tb.Text, 16));
                    }
                    catch (Exception)
                    {
                        LblMessage.Content = "Provide a valid binary input.";
                        TxtDecimal.Text = "ERR";
                        TxtHex.Text = "ERR";
                    }
                }
            }
        } */
    }
}
